package com.xzzz.common.base.config;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 静态资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/apidoc/**")
                .addResourceLocations("classpath:/static/apidoc/")
                .setCacheControl(CacheControl.maxAge(4, TimeUnit.DAYS).cachePublic());

        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/img/favicon.ico")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS).cachePublic())
        ;
    }

    @Bean("jackson2ObjectMapperBuilderCustomizer")
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
        // 序列化枚举是以 toString() 来输出，默认 false 。用来处理 Mybatis-plus 通用枚举类的使用
        .featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
        .serializerByType(Long.class, ToStringSerializer.instance)
        .serializerByType(Long.TYPE, ToStringSerializer.instance)
        .serializerByType(BigInteger.class, ToStringSerializer.instance)
        .serializerByType(BigDecimal.class, ToStringSerializer.instance);
    }

    /**
     * 过滤器方式配置跨域
     * @return 跨域过滤器
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> filterFilterRegistrationBean() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.setAllowCredentials(true);
        // 允许的请求来源
        corsConfig.addAllowedOriginPattern("*");
        // 允许的请求头
        corsConfig.addAllowedHeader("*");
        // 暴露头信息
        corsConfig.addExposedHeader(HttpHeaders.SET_COOKIE);
        // 允许的请求方式
        corsConfig.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig);

        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
        // 过滤器优先级
        bean.setOrder(Integer.MIN_VALUE);
        return bean;
    }
}
